package com.ruoyi.qiushi.service.wechat;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.qiushi.mapper.mall.TradeAfterSaleMapper;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class WechatPay {
    @Autowired(required = false)
    private JsapiServiceExtension jsapiServiceExtension;
    @Autowired(required = false)
    private RefundService refundService;
    @Autowired(required = false)
    private RSAAutoCertificateConfig rsaAutoCertificateConfig;
    @Autowired
    private TradeAfterSaleMapper tradeAfterSaleMapper;

    private String apiV3key;

    @Value("${pay.wechat.base_url}")
    private String notifyUrl;
//    private final String notifyUrl = "http://localhost:8083";

    /**
     * 微信退款
     * https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/create.html
     */
    public void refund(String outRefundNo, String pay_trade_no, Long refund_price, Long actual_price) {
//        Long refundAmount = refund_price * 100;//退款金额 单位：分
//        Long total = actual_price * 100;//原订单总价 单位：分
        Long refundAmount = refund_price;//退款金额 单位：分
        Long total = actual_price;//原订单总价 单位：分

        System.out.println();
        System.out.println("outRefundNo:\t" + outRefundNo);
        System.out.println("pay_trade_no:\t" + pay_trade_no);
        System.out.println("refundAmount:\t" + refundAmount);
        System.out.println("total:\t" + total);
        System.out.println();

        CreateRequest request = new CreateRequest();
        request.setOutRefundNo(outRefundNo);
        request.setOutTradeNo(pay_trade_no);
        request.setNotifyUrl(notifyUrl + "/pay/orderWechatCallback");

        AmountReq amountReq = new AmountReq();
        amountReq.setRefund(refundAmount);
        amountReq.setTotal(total);
        amountReq.setCurrency("CNY");
        request.setAmount(amountReq);

        Refund refund = refundService.create(request);
    }

    /**
     * 退款回调
     * https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/refund-result-notice.html
     */
    public void refundCallBack() {
        Map<String, Object> params = new HashMap<>();//接口接受参数  requestBody
        HttpServletRequest request = null;

        String body = JSONObject.toJSONString(params);
        String signature = request.getHeader("Wechatpay-Signature");
        String nonce = request.getHeader("Wechatpay-Nonce");
        String timestamp = request.getHeader("Wechatpay-Timestamp");
        String serial = request.getHeader("Wechatpay-Serial");
        request.getHeader("Wechatpay-Signature-Type");

        // 构造 RequestParam
        RequestParam requestParam = new RequestParam.Builder()
                .serialNumber(serial)
                .nonce(nonce)
                .signature(signature)
                .timestamp(timestamp)
                .body(body)
                .build();

        NotificationParser parser = new NotificationParser(rsaAutoCertificateConfig);
        RefundNotification transaction = parser.parse(requestParam, RefundNotification.class);
    }
}
